import user  from '@/api/user'

const state = {
  data: {},
}

const mutations = {
  SET_USER_INFO(state, data) {
    state.data = data
  }
}

const actions = {

  // 获取当前登录用户信息
  getUserInfo({commit}) {
    return new Promise((resolve, reject) => {

      if (state.data.id) {
        resolve(state)

      } else {
        user.getMe().then(ret => {
          commit('SET_USER_INFO', ret.data.data)
          resolve(ret)

        }).catch(ret => {
          reject(ret)
        })

      }

    })

  },

}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
